package com.example.myapplication.tack_33.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_30.PaymentPageActivity;
import com.example.myapplication.tack_33.CommentActivity;
import com.example.myapplication.tack_33.OrderDetailsActivity;
import com.example.myapplication.tack_33.data.CaiDanData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class CaiDanAdapter extends RecyclerView.Adapter<CaiDanAdapter.My> {
    private List<CaiDanData.RowsDTO> list;
    private Context context;
    private View view;
    private Gson gson;
    private NetClick netClick;

    public CaiDanAdapter(List<CaiDanData.RowsDTO> list, Context context,NetClick netClick) {
        this.list = list;
        this.context = context;
        this.netClick = netClick;
        gson = new Gson();
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_33_cai_dan,null);
        return new My(view);
    }
    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL+list.get(position).getSellerInfo().getImgUrl()).into(holder.imgUrl);
        Glide.with(context).load(OkHttpTool.URL+list.get(position).getOrderInfo().getOrderItemList().get(0).getProductImage()).into(holder.productImage);
        holder.name.setText(list.get(position).getSellerInfo().getName());
        holder.status.setText(list.get(position).getOrderInfo().getStatus());
        holder.createTime.setText("下单时间："+list.get(position).getOrderInfo().getPayTime());
        holder.amount.setText("总价格："+list.get(position).getOrderInfo().getAmount());
        holder.paymentType.setText("支付方式："+list.get(position).getOrderInfo().getPaymentType());
        holder.quantity.setText("数量："+list.get(position).getOrderInfo().getOrderItemList().get(0).getQuantity());

        if("待支付".equals(list.get(position).getOrderInfo().getStatus())){
            holder.payment.setVisibility(View.VISIBLE);
            holder.comment.setVisibility(View.GONE);
            holder.refund.setVisibility(View.GONE);

        }else if("待评价".equals(list.get(position).getOrderInfo().getStatus())){
            holder.payment.setVisibility(View.GONE);
            holder.refund.setVisibility(View.VISIBLE);
            holder.comment.setVisibility(View.VISIBLE);
        }
        if("退款".equals(list.get(position).getOrderInfo().getStatus())){
            holder.payment.setVisibility(View.GONE);
            holder.comment.setVisibility(View.GONE);
            holder.refund.setVisibility(View.GONE);
        }
        holder.refund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netClick.click(list.get(position).getOrderInfo().getOrderNo());
            }
        });
        holder.payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PaymentPageActivity.class);
                intent.putExtra(Common.COMMON,list.get(position).getOrderInfo().getOrderNo());
                context.startActivity(intent);
            }
        });
        holder.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CommentActivity.class);
                intent.putExtra(Common.COMMON,String.valueOf(list.get(position).getOrderInfo().getOrderNo()));
                context.startActivity(intent);
            }
        });
        holder.line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderDetailsActivity.class);
                intent.putExtra(Common.COMMON,list.get(position).getOrderInfo().getOrderNo());
                context.startActivity(intent);
            }
        });


    }

    public interface NetClick{
        void click(String order);
    }
    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView imgUrl;
        private TextView name;
        private TextView status;
        private ImageView productImage;
        private TextView createTime;
        private TextView quantity;
        private TextView amount;
        private TextView comment;
        private TextView paymentType;
        private TextView refund;
        private TextView payment;
        private LinearLayout line;

        public My(@NonNull View itemView) {
            super(itemView);
            imgUrl = view.findViewById(R.id.imgUrl);
            status = view.findViewById(R.id.status);
            name = view.findViewById(R.id.name);
            productImage = view.findViewById(R.id.productImage);
            createTime = view.findViewById(R.id.createTime);
            quantity = view.findViewById(R.id.quantity);
            amount = view.findViewById(R.id.amount);
            comment = view.findViewById(R.id.comment);
            paymentType = view.findViewById(R.id.paymentType);
            line = view.findViewById(R.id.line);
            payment = view.findViewById(R.id.payment);
            refund = view.findViewById(R.id.refund);
        }
    }
}
